package com.circle.rest.modular.qrtz.listener;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.circle.rest.core.util.SpringUtils;
import com.circle.rest.modular.qrtz.model.Task;
import com.circle.rest.modular.qrtz.service.ITaskService;
import org.quartz.*;

/**
 * @program: RestfulApi
 * @description: 任务总体监听器
 * @author: 赵航
 * @create: 2019-04-12 17:37
 **/
public class GlobalSchedulerListener implements SchedulerListener {

    @SuppressWarnings("unchecked")
    private static ITaskService taskService = SpringUtils.getBean("taskService");

    @Override
    public void jobScheduled(Trigger trigger) {

    }

    @Override
    public void jobUnscheduled(TriggerKey triggerKey) {

    }

    @Override
    public void triggerFinalized(Trigger trigger) {
        String triggerName = trigger.getKey().getName();
        System.out.println(triggerName + "执行完毕");
        Task task = new Task();
        task.setStatus(Trigger.TriggerState.COMPLETE.ordinal());
        UpdateWrapper<Task> wrapper = new UpdateWrapper<>();
        wrapper.eq("name", triggerName);
        taskService.update(task, wrapper);
    }

    @Override
    public void triggerPaused(TriggerKey triggerKey) {
        String triggerName = triggerKey.getName();
        System.out.println(triggerName + "被暂停");
        Task task = new Task();
        task.setStatus(Trigger.TriggerState.PAUSED.ordinal());
        UpdateWrapper<Task> wrapper = new UpdateWrapper<>();
        wrapper.eq("name", triggerName);
        taskService.update(task, wrapper);
    }

    @Override
    public void triggersPaused(String triggerGroup) {
    }

    @Override
    public void triggerResumed(TriggerKey triggerKey) {
        String triggerName = triggerKey.getName();
        System.out.println(triggerName + "被恢复启动");
        Task task = new Task();
        task.setStatus(Trigger.TriggerState.NORMAL.ordinal());
        UpdateWrapper<Task> wrapper = new UpdateWrapper<>();
        wrapper.eq("name", triggerName);
        taskService.update(task, wrapper);
    }

    @Override
    public void triggersResumed(String triggerGroup) {

    }

    @Override
    public void jobAdded(JobDetail jobDetail) {

    }

    @Override
    public void jobDeleted(JobKey jobKey) {

    }

    @Override
    public void jobPaused(JobKey jobKey) {

    }

    @Override
    public void jobsPaused(String jobGroup) {

    }

    @Override
    public void jobResumed(JobKey jobKey) {

    }

    @Override
    public void jobsResumed(String jobGroup) {

    }

    @Override
    public void schedulerError(String msg, SchedulerException cause) {

    }

    @Override
    public void schedulerInStandbyMode() {

    }

    @Override
    public void schedulerStarted() {
    }

    @Override
    public void schedulerStarting() {

    }

    @Override
    public void schedulerShutdown() {

    }

    @Override
    public void schedulerShuttingdown() {

    }

    @Override
    public void schedulingDataCleared() {

    }
}
